package cc.aiee.paypal.config;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.braintreegateway.BraintreeGateway;
import com.braintreegateway.Environment;

public class PayPalConfigKit {

	public static BraintreeGateway defaultBraintreeGateway = null;

	public static Map<String, BraintreeGateway> braintreeGatewayMap;

	public static void put(PayPalConfig payPalConfig, boolean isOpenSandbox) {
		if (braintreeGatewayMap == null) {
			getInstance();
		}
		BraintreeGateway braintreeGateway = null;
		if (isOpenSandbox) {
			braintreeGateway = new BraintreeGateway(Environment.SANDBOX, payPalConfig.getMerchantId(),
					payPalConfig.getPublicKey(), payPalConfig.getPrivateKey());
		} else {
			braintreeGateway = new BraintreeGateway(Environment.PRODUCTION, payPalConfig.getMerchantId(),
					payPalConfig.getPublicKey(), payPalConfig.getPrivateKey());
		}

		braintreeGatewayMap.put(payPalConfig.getMerchantId(), braintreeGateway);
	}

	public static synchronized void getInstance() {
		if (braintreeGatewayMap == null) {
			braintreeGatewayMap = new ConcurrentHashMap<>();
		}
	}

	public static synchronized void setDefault(PayPalConfig payPalConfig, boolean isOpenSandbox) {
		if (isOpenSandbox) {
			defaultBraintreeGateway = new BraintreeGateway(Environment.SANDBOX, payPalConfig.getMerchantId(),
					payPalConfig.getPublicKey(), payPalConfig.getPrivateKey());
		} else {
			defaultBraintreeGateway = new BraintreeGateway(Environment.PRODUCTION, payPalConfig.getMerchantId(),
					payPalConfig.getPublicKey(), payPalConfig.getPrivateKey());
		}
	}

	public static BraintreeGateway getDefault(PayPalConfig payPalConfig, boolean isOpenSandbox) {
		if (defaultBraintreeGateway == null) {
			setDefault(payPalConfig, isOpenSandbox);
		}
		return defaultBraintreeGateway;
	}
}
